package @@packageName@@;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.json.client.*;

public class ListJsonizer {
    public static List<Character> toJavaChar(JSONValue value) {
        return toJava(value, new JavaConverter<Character>() {
            public Character toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaChar(arg0);
            }
        });
    }

    public static List<String> toJavaString(JSONValue value) {
        return toJava(value, new JavaConverter<String>() {
            public String toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaString(arg0);
            }
        });
    }

    public static List<Boolean> toJavaBoolean(JSONValue value) {
        return toJava(value, new JavaConverter<Boolean>() {
            public Boolean toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaBoolean(arg0);
            }
        });
    }

    public static List<Short> toJavaShort(JSONValue value) {
        return toJava(value, new JavaConverter<Short>() {
            public Short toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaShort(arg0);
            }
        });
    }

    public static List<Integer> toJavaInteger(JSONValue value) {
        return toJava(value, new JavaConverter<Integer>() {
            public Integer toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaInteger(arg0);
            }
        });
    }

    public static List<Long> toJavaLong(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Long>() {
            public Long toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaLong(arg0);
            }
        });
    }

    public static List<Float> toJavaFloat(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Float>() {
            public Float toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaFloat(arg0);
            }
        });
    }

    public static List<Double> toJavaDouble(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Double>() {
            public Double toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaDouble(arg0);
            }
        });
    }

    public static List<Date> toJavaDate(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Date>() {
            public Date toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaDate(arg0);
            }
        });
    }

    public static JSONValue toJsonChar(List<Character> arg0) {
        return toJson(arg0, new JsonConverter<Character>() {
            public JSONValue toJson(Character arg0) {
                return ObjectJsonizer.toJsonChar(arg0);
            }
        });
    }

    public static JSONValue toJsonString(List<String> arg0) {
        return toJson(arg0, new JsonConverter<String>() {
            public JSONValue toJson(String arg0) {
                return ObjectJsonizer.toJsonString(arg0);
            }
        });
    }

    public static JSONValue toJsonBoolean(List<Boolean> arg0) {
        return toJson(arg0, new JsonConverter<Boolean>() {
            public JSONValue toJson(Boolean arg0) {
                return ObjectJsonizer.toJsonBoolean(arg0);
            }
        });
    }

    public static JSONValue toJsonShort(List<Short> arg0) {
        return toJson(arg0, new JsonConverter<Short>() {
            public JSONValue toJson(Short arg0) {
                return ObjectJsonizer.toJsonShort(arg0);
            }
        });
    }

    public static JSONValue toJsonInteger(List<Integer> arg0) {
        return toJson(arg0, new JsonConverter<Integer>() {
            public JSONValue toJson(Integer arg0) {
                return ObjectJsonizer.toJsonInteger(arg0);
            }
        });
    }

    public static JSONValue toJsonLong(List<Long> arg0) {
        return toJson(arg0, new JsonConverter<Long>() {
            public JSONValue toJson(Long arg0) {
                return ObjectJsonizer.toJsonLong(arg0);
            }
        });
    }

    public static JSONValue toJsonFloat(List<Float> arg0) {
        return toJson(arg0, new JsonConverter<Float>() {
            public JSONValue toJson(Float arg0) {
                return ObjectJsonizer.toJsonFloat(arg0);
            }
        });
    }

    public static JSONValue toJsonDouble(List<Double> arg0) {
        return toJson(arg0, new JsonConverter<Double>() {
            public JSONValue toJson(Double arg0) {
                return ObjectJsonizer.toJsonDouble(arg0);
            }
        });
    }

    public static JSONValue toJsonDate(List<Date> arg0) {
        JSONValue jsonValue = toJson(arg0, new JsonConverter<Date>() {
            public JSONValue toJson(Date arg0) {
                return ObjectJsonizer.toJsonDate((Date) arg0);
            }
        });

        return jsonValue;
    }

    private static <T> List<T> toJava(JSONValue arg0, JavaConverter<T> converter) {
        if (arg0.isNull() != null) {
            return null;
        }

        JSONArray jsonArray = arg0.isArray();
        if (jsonArray == null) {
            throw new JSONException("expected JSONNull or JSONArray");
        }

        List<T> list = new ArrayList<T>();
        for (int i = 0, n = jsonArray.size(); i < n; i++) {
            list.add(converter.toJava(jsonArray.get(i)));
        }
        return list;
    }

    private static <T> JSONValue toJson(List<T> arg0, JsonConverter<T> converter) {
        if (arg0 == null) {
            return JSONNull.getInstance();
        }

        JSONArray jsonArray = new JSONArray();
        for (int i = 0, n = arg0.size(); i < n; i++) {
            jsonArray.set(i, converter.toJson(arg0.get(i)));
        }
        return jsonArray;
    }

    private interface JavaConverter<T> {
        public T toJava(JSONValue arg0);
    }

    private interface JsonConverter<T> {
        public JSONValue toJson(T arg0);
    }
}
